<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>物业管理系统</title>
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/AdminLTE.min.css">
</head>
<style>
</style>
<body style="padding-bottom: 90px; background-color: #FFF;">
	<div class="modal-content">
		<div class="modal-body">
			<div class="form-group">
				<label class="control-label">区域：<!-- <span style="color:#F00">*</span> --></label> <select id="r_id" class="form-control">

				</select>
			</div>
			<div class="form-group">
				<label class="control-label">编号：</label> <input type="text" class="form-control" id="number" placeholder="示例 001,010,101" onBlur="checkingNumber()">
			</div>
			<div class="form-group">
				<label class="control-label">出售价格(元)：</label> <input type="text" class="form-control" id="sellPrice">
			</div>
			<div class="form-group">
				<label class="control-label">使用时限(年)：</label> <input type="text" class="form-control" id="timeLimit" placeholder="示例 001,010,101">
			</div>
		</div>
	</div>
</body>
<script src="${pageContext.request.contextPath}/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/js/date.js"></script>
<!-- layer -->
<script src="${pageContext.request.contextPath}/plugins/layer/2.4/layer.js"></script>
<script>
	var lock = false;
	var id = "${map['id']}";
	var index = parent.layer.getFrameIndex(window.name);
    var pattern = /^[0-9]*$/;//数字zhengze
	var checkNumber=false;
	var checkNumber = false;
	$(function() {
		$.ajax({
					url : "${pageContext.request.contextPath}/selectParkingRegionByPage",
					type : "post",
					dataType : "json",
					data : {
						offset : 0,
						limit : 999,
						name : ''
					},
					success : function(data) {
						var parkingRegion = data['rows'];
						var html = "<option value=''>请选择...</option>";
						for (var i = 0; i < parkingRegion.length; i++) {
							html += "<option value='"+parkingRegion[i].id+"'>"
									+ parkingRegion[i].regionName + "</option>";
						}
						$("#r_id").html(html);
						if (id != '') {
							ajaxData();
						}
					}
				});
	});

	function ajaxData() {
		$.ajax({
			url : "${pageContext.request.contextPath}/selectParkingSpaceById",
			type : "post",
			data : {
				id : id
			},
			dataType : "json",
			success : function(map) {
				var parkingSpace = map['parkingSpace'];
				$("#r_id").val(parkingSpace.rId);
				$("#number").val(parkingSpace.number);
				$("#state").val(parkingSpace.state);
				$("#sellPrice").val(parkingSpace.sellPrice);
				$("#timeLimit").val(parkingSpace.timeLimit);
			}
		});
	}

	function checkingNumber() {
		checkNumber = false;
		if (id == '') {
			$
					.ajax({
						url : "${pageContext.request.contextPath}/checkingParkingSpaceNumber",
						type : "post",
						data : {
							number : $("#number").val(),
							rId : $("#r_id").val()
						},
						dataType : "json",
						success : function(data) {
							var result = data['result'];
							if (result == 0) {
								checkNumber = true;
							}
						},
						error : function() {
							layer.msg('系统繁忙，请稍后再试!', {
								icon : 2,
								time : 800
							});
						}
					});
		}
	}

	//添加或修改
	function insertData() {
		lock = true;
		var rId = $("#r_id").val();
		var number = $("#number").val();
		var sellPrice=$("#sellPrice").val();
		var timeLimit=$("#timeLimit").val();
		if (rId === '' || number === '') {
			layer.alert("数据填写不完整!");
			lock = false;
			return;
		}
		if( !pattern.test(number)|| !pattern.test(sellPrice)|| !pattern.test(timeLimit)){
			layer.alert("编号、价格、时限填写整数!");
			lock = false;
			return;
		}
		if (checkNumber) {
			layer.alert("编号重复!");
			lock = false;
			return;
		}
		var url = id === '' ? '${pageContext.request.contextPath}/insertParkingSpace': '${pageContext.request.contextPath}/updateParkingSpace';
		$.ajax({
			url : url,
			type : "post",
			data : {
				id : id,
				rId : rId,
				number : number,
				sellPrice:sellPrice,
				timeLimit:timeLimit
			},
			dataType : "json",
			success : function(data) {
				var result = data['result'];
				if (result == 1) {
					layer.msg('操作成功!', {
						icon : 1,
						time : 800
					});
					setTimeout(function() {
						parent.querry();
						parent.layer.close(index);
					}, 1000);
				} else {
					layer.msg('操作失败!', {
						icon : 2,
						time : 800
					});
					lock = false;
				}
			}
		});
	}

	function insert() {
		if (!lock) {
			insertData();
		}
	}
</script>
</html>
